Object-Oriented Programming (OOPs) Interview

━━━━━━━━━━━━━━━━━━━━━━
1. What is OOP?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Object-Oriented Programming (OOP) is a programming paradigm based on objects rather than functions.

Objects contain both:

• Data (Attributes)
• Functions (Methods)

OOP helps in developing software that is modular, reusable, secure, and easy to maintain.

Examples of OOP Languages:

• Java
• Python
• C++
• C#
• Kotlin

━━━━━━━━━━━━━━━━━━━━━━
2. Why do we use OOP?
━━━━━━━━━━━━━━━━━━━━━━

OOP is used because it:

• Improves code reusability
• Makes maintenance easier
• Provides better security
• Reduces code duplication
• Makes programs modular
• Helps manage large applications

━━━━━━━━━━━━━━━━━━━━━━
3. What are the Four Pillars of OOP?
━━━━━━━━━━━━━━━━━━━━━━

1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism

This is one of the most frequently asked interview questions.

━━━━━━━━━━━━━━━━━━━━━━
4. What is a Class?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

A Class is a blueprint or template used to create objects.

It defines:

• Variables (Attributes)
• Methods (Functions)

Example (Python):

class Student:
    name = ""

Here, Student is the Class.

━━━━━━━━━━━━━━━━━━━━━━
5. What is an Object?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

An Object is an instance of a class.

Objects have:

• State
• Behavior

Example:

s1 = Student()

Here, s1 is an Object.

━━━━━━━━━━━━━━━━━━━━━━
6. Difference Between Class and Object
━━━━━━━━━━━━━━━━━━━━━━

Class

• Blueprint
• No memory allocated
• Defines properties and methods

Object

• Instance of a class
• Memory allocated
• Uses the properties and methods

━━━━━━━━━━━━━━━━━━━━━━
7. What is Encapsulation?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Encapsulation means binding data and methods together into a single unit (class) while restricting direct access to data.

It protects data from unauthorized access.

Real-Life Example:

ATM Machine

You can withdraw money but cannot directly access the bank database.

Advantages:

• Data Security
• Data Hiding
• Better Maintenance

━━━━━━━━━━━━━━━━━━━━━━
8. What is Abstraction?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Abstraction means showing only essential information while hiding internal implementation details.

Real-Life Example:

Driving a Car

You use:

• Accelerator
• Brake
• Steering

You don't need to know how the engine works internally.

Advantages:

• Hides complexity
• Improves security
• Easy to use

━━━━━━━━━━━━━━━━━━━━━━
Difference Between Encapsulation and Abstraction
━━━━━━━━━━━━━━━━━━━━━━

Encapsulation

• Hides data
• Achieved using classes
• Focuses on security

Abstraction

• Hides implementation
• Achieved using abstract classes/interfaces
• Focuses on simplicity


━━━━━━━━━━━━━━━━━━━━━━
9. What is Inheritance?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Inheritance allows one class to acquire the properties and methods of another class.

A Child class inherits from a Parent class.

Advantages:

• Code Reusability
• Less Code
• Easy Maintenance

Types of Inheritance:

• Single
• Multiple
• Multilevel
• Hierarchical
• Hybrid

Example:

Animal
   ↓
Dog

Dog inherits the properties of Animal.

━━━━━━━━━━━━━━━━━━━━━━
10. What is Polymorphism?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Polymorphism means "One Interface, Many Forms."

The same method behaves differently in different situations.

Types:

• Compile-Time Polymorphism
• Run-Time Polymorphism

━━━━━━━━━━━━━━━━━━━━━━
11. What is Method Overloading?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Method Overloading means having the same method name with different parameters.

Example:

add(int, int)

add(int, int, int)

Note:

• Supported directly in Java and C++.
• Python does not support traditional method overloading.

━━━━━━━━━━━━━━━━━━━━━━
12. What is Method Overriding?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Method Overriding occurs when a child class provides its own implementation of a method already defined in the parent class.

Example:

Animal
   ↓
Dog

Both classes have sound() method.

Dog overrides sound().

━━━━━━━━━━━━━━━━━━━━━━
Difference Between Method Overloading and Method Overriding
━━━━━━━━━━━━━━━━━━━━━━

Method Overloading

• Same method name
• Different parameters
• Compile-Time
• Same class

Method Overriding

• Same method name
• Same parameters
• Run-Time
• Parent and Child classes

━━━━━━━━━━━━━━━━━━━━━━
13. What is a Constructor?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

A Constructor is a special method that is automatically called when an object is created.

Python:

__init__()

Java:

Student()

Purpose:

Initialize object values automatically.

━━━━━━━━━━━━━━━━━━━━━━
14. Types of Constructors
━━━━━━━━━━━━━━━━━━━━━━

1. Default Constructor

A constructor with no parameters.

2. Parameterized Constructor

A constructor that accepts parameters to initialize object values.

━━━━━━━━━━━━━━━━━━━━━━
15. What is this / self?
━━━━━━━━━━━━━━━━━━━━━━

Java

this

• Refers to the current object.

Python

self

• Represents the current object.
• Used to access instance variables and methods.

━━━━━━━━━━━━━━━━━━━━━━
16. What is an Interface?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

An Interface contains only method declarations.

It provides complete abstraction.

It is mainly used to achieve multiple inheritance in Java.

━━━━━━━━━━━━━━━━━━━━━━
17. What is an Abstract Class?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

An Abstract Class cannot be instantiated (objects cannot be created).

It can contain:

• Abstract Methods
• Normal Methods

Difference:

Interface

• Mostly contains method declarations.

Abstract Class

• Can contain both implemented and abstract methods.

━━━━━━━━━━━━━━━━━━━━━━
Difference Between Interface and Abstract Class
━━━━━━━━━━━━━━━━━━━━━━

Interface

• Cannot create objects
• Mostly method declarations
• Supports multiple inheritance

Abstract Class

• Cannot create objects
• Can contain implemented methods
• Provides partial abstraction


━━━━━━━━━━━━━━━━━━━━━━
18. What is Association?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Association is a relationship where two independent objects work together.

Both objects can exist independently.

Example:

Teacher ↔ Student

A Teacher can exist without a Student, and a Student can exist without a Teacher.

━━━━━━━━━━━━━━━━━━━━━━
19. What is Aggregation?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Aggregation is a weak "Has-A" relationship.

The child object can exist independently of the parent object.

Example:

Department
     ↓
Professor

A Professor can exist even if the Department is removed.

━━━━━━━━━━━━━━━━━━━━━━
20. What is Composition?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Composition is a strong "Has-A" relationship.

The child object cannot exist independently of the parent object.

Example:

House
   ↓
Rooms

Without the House, the Rooms have no meaning.

━━━━━━━━━━━━━━━━━━━━━━
Difference Between Aggregation and Composition
━━━━━━━━━━━━━━━━━━━━━━

Aggregation

• Weak relationship
• Child exists independently
• Parent and child have separate lifecycles

Composition

• Strong relationship
• Child depends on parent
• Child is destroyed when parent is destroyed

━━━━━━━━━━━━━━━━━━━━━━
21. What are Access Modifiers?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Access Modifiers control the visibility of variables, methods, and classes.

Types:

Public

• Accessible from anywhere.

Private

• Accessible only inside the same class.

Protected

• Accessible inside the class and its subclasses.

━━━━━━━━━━━━━━━━━━━━━━
22. What is Data Hiding?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Data Hiding is the process of preventing direct access to class variables.

It is achieved using private variables and controlled access through methods.

Advantages:

• Improves Security
• Prevents Unauthorized Access
• Protects Sensitive Data

━━━━━━━━━━━━━━━━━━━━━━
23. What is Dynamic Binding?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Dynamic Binding means the method call is resolved during runtime.

It occurs in Method Overriding.

Example:

A parent reference calls a child class method, and the decision is made at runtime.

━━━━━━━━━━━━━━━━━━━━━━
24. What is Static Binding?
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Static Binding means the method call is resolved during compile time.

It commonly occurs in Method Overloading (in languages that support it).

Advantages:

• Faster execution
• Less runtime overhead

━━━━━━━━━━━━━━━━━━━━━━
25. Object vs Instance
━━━━━━━━━━━━━━━━━━━━━━

Answer:

Both terms are often used interchangeably.

Object

• A real entity created from a class.

Instance

• Refers to a specific object created from a class.

Example:

Student s1 = new Student();

Here, s1 is both an Object and an Instance of the Student class.

━━━━━━━━━━━━━━━━━━━━━━
26. Real-Life Examples of the Four Pillars of OOP
━━━━━━━━━━━━━━━━━━━━━━

Encapsulation

Example:

ATM Machine

• Internal data is hidden.
• Users can perform only allowed operations.

━━━━━━━━━━━━━━━━━━━━━━

Abstraction

Example:

Car

• Drive the car without knowing how the engine works internally.

━━━━━━━━━━━━━━━━━━━━━━

Inheritance

Example:

Vehicle
   ↓
Car
Bike

Both Car and Bike inherit properties from Vehicle.

━━━━━━━━━━━━━━━━━━━━━━

Polymorphism

Example:

Person

• Teacher at School
• Parent at Home
• Customer in a Bank

Same person, different roles and behaviors.

━━━━━━━━━━━━━━━━━━━━━━
27. Difference Between Class and Structure
━━━━━━━━━━━━━━━━━━━━━━

Class

• Supports OOP
• Can contain methods
• Supports inheritance
• Provides data hiding

Structure

• Mainly stores data
• Traditionally has no methods (in C)
• Does not support inheritance
• Less secure than classes

Object-Oriented Programming (OOPs) Interview Guide – Part 4

━━━━━━━━━━━━━━━━━━━━━━
28. Advantages of OOP
━━━━━━━━━━━━━━━━━━━━━━

• Code Reusability
• Better Security
• Easy Maintenance
• Modularity
• Flexibility
• Scalability
• Reduced Code Duplication
• Easier Debugging
• Better Code Organization

━━━━━━━━━━━━━━━━━━━━━━
29. Disadvantages of OOP
━━━━━━━━━━━━━━━━━━━━━━

• More Memory Usage
• Slightly Slower than Procedural Programming
• Can Be Complex for Small Programs
• Requires Proper Design
• Higher Initial Development Time

━━━━━━━━━━━━━━━━━━━━━━
30. Real-World Example of Class and Object
━━━━━━━━━━━━━━━━━━━━━━

Class

Car

Objects

• BMW
• Audi
• Tesla

Explanation:

Car is the blueprint (Class).

BMW, Audi, and Tesla are Objects created from the Car class.

━━━━━━━━━━━━━━━━━━━━━━
Most Frequently Asked OOP Interview Questions
━━━━━━━━━━━━━━━━━━━━━━

1. What is OOP?

2. What are the Four Pillars of OOP?

3. What is a Class?

4. What is an Object?

5. Difference Between Class and Object.

6. What is Encapsulation?

7. What is Abstraction?

8. Difference Between Encapsulation and Abstraction.

9. What is Inheritance?

10. Types of Inheritance.

11. What is Polymorphism?

12. Difference Between Method Overloading and Method Overriding.

13. What is a Constructor?

14. Default Constructor vs Parameterized Constructor.

15. What is this (Java) / self (Python)?

16. What is an Interface?

17. What is an Abstract Class?

18. Difference Between Interface and Abstract Class.

19. Difference Between Aggregation and Composition.

20. What are Access Modifiers?

21. What is Data Hiding?

22. Difference Between Static Binding and Dynamic Binding.

23. Advantages of OOP.


━━━━━━━━━━━━━━━━━━━━━━
Quick Revision
━━━━━━━━━━━━━━━━━━━━━━

✔ OOP → Programming based on objects.

✔ Class → Blueprint for creating objects.

✔ Object → Instance of a class.

✔ Encapsulation → Binding data and methods together.

✔ Abstraction → Hiding implementation details.

✔ Inheritance → Child class inherits from parent class.

✔ Polymorphism → One interface, many forms.

✔ Method Overloading → Same method, different parameters.

✔ Method Overriding → Child class redefines parent method.

✔ Constructor → Initializes objects automatically.

✔ this → Refers to current object in Java.

✔ self → Refers to current object in Python.

✔ Interface → Contains method declarations; provides abstraction.

✔ Abstract Class → Can contain both abstract and implemented methods.

✔ Association → Two independent objects work together.

✔ Aggregation → Weak "Has-A" relationship.

✔ Composition → Strong "Has-A" relationship.

✔ Public → Accessible everywhere.

✔ Private → Accessible only within the class.

✔ Protected → Accessible within the class and subclasses.

✔ Data Hiding → Restricts direct access to data.

✔ Dynamic Binding → Runtime method resolution.

✔ Static Binding → Compile-time method resolution.

━━━━━━━━━━━━━━━━━━━━━━
Interview Tip
━━━━━━━━━━━━━━━━━━━━━━

If the interviewer asks:

"Which OOP concept do you use most?"

Sample Answer:

"In my projects, I primarily use Encapsulation to organize data and methods within classes, and Inheritance to reuse code and avoid duplication. I also use Polymorphism to write flexible and maintainable code by allowing the same interface to have different implementations. These concepts make applications easier to develop, test, and maintain."

This preparation covers almost all OOP questions commonly asked in TCS, Infosys, Accenture, Capgemini, Cognizant, Wipro, Deloitte, HCL, and other service-based company interviews.